package de.gwtprime.client.widgets.trading;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.requestfactory.shared.Receiver;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.event.OrderProcessedEvent;
import de.gwtprime.client.event.handler.OrderProcessedEventHandler;
import de.gwtprime.client.viewsStatic.LoadingIndictator;
import de.gwtprime.client.widgets.trading.TradingTable.BuySellTable;
import de.gwtprime.shared.requestFactory.OrderProxy;
import de.gwtprime.shared.requestFactory.OrderProxy.OrderType;

public class TradingOverview extends Composite {

	private static TradingOverviewUiBinder uiBinder = GWT
			.create(TradingOverviewUiBinder.class);

	interface TradingOverviewUiBinder extends UiBinder<Widget, TradingOverview> {}

	@UiField SimplePanel buyOrderTable;
	@UiField SimplePanel sellOrderTable;
	@UiField SimplePanel tradingWidget;
	@UiField SimplePanel loadingOverview;
	
	TradingTable buyTable;
	TradingTable sellTable;
	
	public TradingOverview(final ClientFactory clientFactory) {
		initWidget(uiBinder.createAndBindUi(this));
		
		
		loadingOverview.add(new LoadingIndictator());
		
		clientFactory.getRequestFactory().orderRequest().loadOrders().fire(new Receiver<List<OrderProxy>>() {

			@Override
			public void onSuccess(List<OrderProxy> response) {
				
				
				
				
				List<OrderProxy> buyOrders = new ArrayList<OrderProxy>();
				List<OrderProxy> sellOrders = new ArrayList<OrderProxy>();
				
				for(OrderProxy order : response){
					
					if(order.getType()==OrderType.BUY)
						buyOrders.add(order);
					else
						sellOrders.add(order);
					
				}
				
				loadingOverview.setVisible(false);
				
				buyTable = new TradingTable(clientFactory, buyOrders, BuySellTable.BUY);
				sellTable = new TradingTable(clientFactory, sellOrders, BuySellTable.SELL);
				
//				
				buyOrderTable.add(buyTable);
				sellOrderTable.add(sellTable);
			}
		});
		
		
		
		
		
		
		clientFactory.getEventBus().addHandler(OrderProcessedEvent.TYPE, new OrderProcessedEventHandler() {
			
			@Override
			public void onOrderProcessed(OrderProcessedEvent event) {
				clientFactory.getRequestFactory().orderRequest().loadOrders().fire(new Receiver<List<OrderProxy>>() {

					@Override
					public void onSuccess(List<OrderProxy> response) {
						
						List<OrderProxy> buyOrders = new ArrayList<OrderProxy>();
						List<OrderProxy> sellOrders = new ArrayList<OrderProxy>();
						
						for(OrderProxy order : response){
							
							if(order.getType()==OrderType.BUY)
								buyOrders.add(order);
							else
								sellOrders.add(order);
							
						}
						
						if(buyTable!=null)
							buyTable.updateOrders(buyOrders);
						
						if(sellTable!=null)
							sellTable.updateOrders(sellOrders);
					}
				});
				
			}
		});
		
		tradingWidget.add(new TradeWidget(clientFactory));
	}

}
